package com.ideal.manage.esms.controller.project;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.*;
import com.ideal.manage.esms.bean.sys.Parameter;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.ProjectService;
import com.ideal.manage.esms.service.system.ParameterService;
import com.ideal.manage.esms.service.system.RoleMenuService;
import com.ideal.manage.esms.service.system.RoleService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 83730 on 2018/4/17.
 */
@Controller
@RequestMapping("project/indexManage")
public class IndexManageController {
    @Resource
    private ProjectService projectService;
    @Resource
    private RoleService roleService;
    @Resource
    private ParameterService parameterService;
    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private MonitorService monitorService;

    //参数测点
    @RequestMapping("index_list")
    public void indexList(Model model) { }

    /**
     * 参数测点数据回显
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("index_data")
    @ResponseBody
    public PageDto indexData(int pageNum, HttpServletRequest request) {
        Page<ParameterInfo> page = projectService.findAllParameterInfos(pageNum,request);
        List<ParameterInfo> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }
    /**
     * 参数测点的数据删除
     * @param ids
     * @return
     */
    @RequestMapping("index_del")
    @ResponseBody
    public Result indexDel(String[] ids){
        projectService.delParameterInfo(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

    /**
     * 参数测点的数据增加
     * @param model
     */
    @RequestMapping("index_add")
    public void indexAdd(Model model) {
        List<Parameter> objNames = parameterService.findChildByParentCode("1010");
        List<Parameter> propertyTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        List<Parameter>  objTypes= parameterService.findChildByParentCode("1030");
        model.addAttribute("objNames", objNames);
        model.addAttribute("propertyTypes", propertyTypes);
        model.addAttribute("sources", sources);
        model.addAttribute("objTypes", objTypes);
    }

    /**
     * 参数测点的数据保存
     * @param id
     * @param code
     * @param name
     * @param objName
     * @param objType
     * @param propertyType
     * @param unit
     * @param source
     * @param isShow
     * @return
     */
    @RequestMapping("index_save")
    @ResponseBody
    public Result indexSave(String id, String code, String name, Integer objName, Integer objType, Integer propertyType, String unit,
                            Integer source, Integer isShow) {
        projectService.saveParameterInfo(id,code,name,objName,objType,propertyType,unit,source,isShow);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 参数测点的数据编辑
     * @param id
     * @param model
     */
    @RequestMapping("index_edit")
    public void indexEdit(String id, Model model) {
        ParameterInfo code = projectService.findById(id);//通过id查询
        model.addAttribute("code", code);

        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);

    }



    /**
     * 导出数据
     */
    @RequestMapping("index_importData")
    public void indexImportData() {

    }
    /**
     * 导入数据
     */
    @RequestMapping("indexData_save")
    @ResponseBody
    public Result indexDataSave() {
        return null;
    }

    //过程告警
    @RequestMapping("warn_list")
    public void warnList(Model model) {
        System.out.println(3333);
    }

    @RequestMapping("warn_data")
    @ResponseBody
    public PageDto warnData(int pageNum, HttpServletRequest request) {

        List<ProcessAlarm> alarms = new ArrayList<>();

        ProcessAlarm alarm = new ProcessAlarm();
        alarm.setNumber("B001");
        alarm.setObject("电池包");
        alarm.setName("电池包过牙一级");
        alarm.setDealWay("现场处理");
        alarm.setLevel("一级");
        alarm.setRecord("否");
        alarm.setTarget("");
        alarm.setTime("20s");

        ProcessAlarm alarm1 = new ProcessAlarm();
        alarm1.setNumber("B002");
        alarm1.setObject("电池包");
        alarm1.setName("单体欠压");
        alarm1.setDealWay("现场处理");
        alarm1.setLevel("一级");
        alarm1.setRecord("是");
        alarm1.setTarget("所有单体电压");
        alarm1.setTime("10s");

        alarms.add(alarm);
        alarms.add(alarm1);

        PageDto pageDto = new PageDto();
        pageDto.setRows(alarms);
        pageDto.setTotal(2L);
        return pageDto;
        /*
         Page<ProcessAlarm> page = projectService.findAll(pageNum,request);
        List<ProcessAlarm> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;

         */
    }

    @RequestMapping("warn_add")
    public void warnAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("warn_edit")
    public void warnEdit(Long id, Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("warn_save")
    @ResponseBody
    public Result warnSave() {

        return null;
    }

    @RequestMapping("warn_importData")
    public void warnImportData() {

    }

    @RequestMapping("warnData_save")
    @ResponseBody
    public Result warnDataSave() {
        return null;
    }


    //设备状态
    @RequestMapping("status_list")
    public void statusList(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("status_data")
    @ResponseBody
    public PageDto statusData(int pageNum, HttpServletRequest request) {
        List<EquipmentStatus> statuses = new ArrayList<>();

        EquipmentStatus status = new EquipmentStatus();
        status.setStatus("关机");
        status.setName("工作状态");
        status.setNumber("Z001");
        status.setObject("变流器");
        status.setSource("通信");

        EquipmentStatus status1 = new EquipmentStatus();
        status1.setStatus("离线");
        status1.setName("交流断路器  ");
        status1.setNumber("Z002");
        status1.setObject("变流器");
        status1.setSource("通信");

        statuses.add(status);
        statuses.add(status1);

        PageDto pageDto = new PageDto();
        pageDto.setRows(statuses);
        pageDto.setTotal(2L);
        return pageDto;
    }

    @RequestMapping("status_add")
    public void statusAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("status_edit")
    public void statusEdit(Long id, Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("status_save")
    @ResponseBody
    public Result statusSave() {
        return null;
    }

    @RequestMapping("status_importData")
    public void statusImportData() {

    }

    @RequestMapping("statusData_save")
    @ResponseBody
    public Result statusDataSave() {
        return null;
    }

    //台账属性
    @RequestMapping("property_list")
    public void propertyList(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("property_data")
    @ResponseBody
    public PageDto propertyData(int pageNum, HttpServletRequest request) {

        List<AccountProperty> properties = new ArrayList<>();

        AccountProperty property = new AccountProperty();
        property.setObject("项目");
        property.setName("项目名称");
        property.setNumber("T001");
        property.setModify("字符型");
        property.setType("否");

        AccountProperty property1 = new AccountProperty();
        property1.setObject("电池包");
        property1.setName("供应商");
        property1.setNumber("T005");
        property1.setModify("是");
        property1.setType("字符型");

        properties.add(property);
        properties.add(property1);

        PageDto pageDto = new PageDto();
        pageDto.setRows(properties);
        pageDto.setTotal(2L);
        return pageDto;
    }

    @RequestMapping("property_add")
    public void propertyAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("property_edit")
    public void propertyEdit(Long id, Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("property_save")
    @ResponseBody
    public Result propertySave() {
        return null;
    }

    @RequestMapping("property_importData")
    public void propertyImportData() {

    }

    @RequestMapping("propertyData_save")
    @ResponseBody
    public Result propertyDataSave() {
        return null;
    }

    //控制指令
    @RequestMapping("order_list")
    public void orderList(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("order_data")
    @ResponseBody
    public PageDto orderData(int pageNum, HttpServletRequest request) {
        List<ControlOrder> orders = new ArrayList<>();

        ControlOrder order = new ControlOrder();
        order.setNumber("K001");
        order.setName("打开正极继电器");
        order.setObject("电池包");
        order.setOperate("是");

        ControlOrder order1 = new ControlOrder();
        order1.setNumber("K002");
        order1.setName("关闭整机继电器");
        order1.setObject("变流器");
        order1.setOperate("否");

        orders.add(order);
        orders.add(order1);

        PageDto pageDto = new PageDto();
        pageDto.setRows(orders);
        pageDto.setTotal(2L);
        return pageDto;
    }

    @RequestMapping("order_add")
    public void orderAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("order_edit")
    public void orderEdit(Long id, Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("order_save")
    @ResponseBody
    public Result orderSave() {
        return null;
    }

    @RequestMapping("order_importData")
    public void orderImportData() {

    }

    @RequestMapping("threshold_list")
    public void shresholdList(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("threshold_data")
    @ResponseBody
    public PageDto shresholdData(int pageNum, HttpServletRequest request) {
        List<ControlOrder> orders = new ArrayList<>();

        ControlOrder order = new ControlOrder();
        order.setNumber("K001");
        order.setName("打开正极继电器");
        order.setObject("电池包");
        order.setOperate("是");

        ControlOrder order1 = new ControlOrder();
        order1.setNumber("K002");
        order1.setName("关闭整机继电器");
        order1.setObject("变流器");
        order1.setOperate("否");

        orders.add(order);
        orders.add(order1);

        PageDto pageDto = new PageDto();
        pageDto.setRows(orders);
        pageDto.setTotal(2L);
        return pageDto;
    }

    @RequestMapping("threshold_add")
    public void thresholdAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("threshold_edit")
    public void thresholdEdit(Long id, Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("orderData_save")
    @ResponseBody
    public Result orderDataSave() {
        return null;
    }

    @RequestMapping("type_list")
    public void typeList(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("type_data")
    @ResponseBody
    public PageDto typeData(int pageNum, HttpServletRequest request) {
        List<ControlOrder> orders = new ArrayList<>();

        ControlOrder order = new ControlOrder();
        order.setNumber("K001");
        order.setName("打开正极继电器");
        order.setObject("电池包");
        order.setOperate("是");

        ControlOrder order1 = new ControlOrder();
        order1.setNumber("K002");
        order1.setName("关闭整机继电器");
        order1.setObject("变流器");
        order1.setOperate("否");

        orders.add(order);
        orders.add(order1);

        PageDto pageDto = new PageDto();
        pageDto.setRows(orders);
        pageDto.setTotal(2L);
        return pageDto;
    }

    @RequestMapping("type_add")
    public void typeAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }

    @RequestMapping("type_edit")
    public void typeEdit(Long id, Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        List<Parameter> dataTypes = parameterService.findChildByParentCode("1000");
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("objects", objects);
        model.addAttribute("dataTypes", dataTypes);
        model.addAttribute("sources", sources);
    }
}
